﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;

namespace OpenSoftStore.Client.UIL.UserControls
{
    /// <summary>
    /// 对象名称：自定义的NumericUpDown控件
    /// 对象说明：相对于原生的NumericUpDown控件，具备了更好的UI显示效果。
    /// 作者姓名：
    /// 编写日期：
    /// </summary>
    [DefaultProperty("NumericUpDown")]
    public partial class NumericUpDown : UserControl
    {
        [Browsable(true)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
        public event EventHandler ValueChanged;

        protected virtual void OnValueChanged(object sender, EventArgs e)
        {
            EventHandler handler = ValueChanged;
            if (handler != null) handler(this, EventArgs.Empty);
        }

        //
        // 摘要:
        //     在以某种方式更改 System.Windows.Forms.NumericUpDown.Value 属性后发生。
        /// <summary>
        /// 自定义NumericUpDown控件的默认实例化方法。使用本自定义Button控件前，请确保
        /// 应用程序的Properties.Resources资源文件中，包含了1张名为Input的图片。
        /// </summary>
        public NumericUpDown()
        {
            InitializeComponent();
            NmrInside.BackColor = Color.White;
            NmrInside.Location = new Point(2, 1);
            NmrInside.Width = PnlWhiteBG.Width - 1;
            NmrInside.GotFocus += new EventHandler(TxtBox_GotFocus);
            NmrInside.Leave += new EventHandler(TxtBox_Leave);
            NmrInside.KeyDown += new KeyEventHandler(KeyDowns);
            NmrInside.KeyPress += new KeyPressEventHandler(KeyPresss);
            NmrInside.KeyUp += new KeyEventHandler(KeyUps);
            NmrInside.Enter += new EventHandler(Enters);
            NmrInside.Leave += new EventHandler(Leaves);
            NmrInside.ValueChanged +=new EventHandler(ValueChangeds);
        }
        private void ValueChangeds(object sender, EventArgs e)
        {
            this.OnValueChanged(sender,e);
        }

        private void Leaves(object sender, EventArgs e)
        {
            this.OnLeave(e);
        }

        private void Enters(object sender, EventArgs e)
        {
            BackColor = Color.FromArgb(179, 221, 150);
            PnlBorder.BackColor = Color.FromArgb(87, 152, 43);
            NmrInside.ForeColor = Color.FromArgb(51, 102, 0);
            //NmrInside.Select();
            //NmrInside.Click(null,null);
            NmrInside.Select(0, NmrInside.Text.Length);
            this.OnEnter(e);
        }
        public void KeyUps(object sender, KeyEventArgs e)
        {
            var findForm = FindForm();
            if (findForm == null) return;
            if (e.KeyCode == Keys.Enter)
            {
                findForm.SelectNextControl(this, true, true, true, true);
            }
            this.OnKeyUp(e);
        }
        public void KeyPresss(object sender, KeyPressEventArgs e)
        {
            this.OnKeyPress(e);
        }
        public void KeyDowns(object sender, KeyEventArgs e)
        {
            this.OnKeyDown(e);
        }

        /// <summary>
        /// 获取或设置文本框中的内容。
        /// </summary>
        [Category("设置")]
        [Description("获取或设置文本框中的内容。")]
        [Browsable(true)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
        public override string Text
        {
            get
            {
                return NmrInside.Text;
            }
            set
            {
                NmrInside.Text = value;
            }
        }

        /// <summary>
        /// 获取或设置文本框中的内容。
        /// </summary>
        [Category("设置")]
        [Description("获取或设置文本框中的内容。")]
        [Browsable(true)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
        public bool ReadOnly
        {
            get
            {
                return NmrInside.ReadOnly;
            }
            set
            {
                NmrInside.ReadOnly = value;
                if (NmrInside.ReadOnly)
                {
                    NmrInside.BackColor = Color.WhiteSmoke;
                    PnlWhiteBG.BackColor = Color.WhiteSmoke;
                    PnlImageBG.BackColor = Color.WhiteSmoke;
                }
                else
                {
                    NmrInside.BackColor = Color.White;
                    PnlWhiteBG.BackColor = Color.White;
                    PnlImageBG.BackColor = Color.White;
                }
            }
        }
        /// <summary>
        /// 获取或设置文本框中的内容。
        /// </summary>
        [Category("设置")]
        [Description("获取或设置文本框中的内容。")]
        [Browsable(true)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
        public int DecimalPlaces
        {
            get
            {
                return NmrInside.DecimalPlaces;
            }
            set
            {
                NmrInside.DecimalPlaces = value;
            }
        }

        public decimal Minimum
        {           
            get
            {
                return NmrInside.Minimum;
            }
            set
            {
                NmrInside.Minimum = value;
            } 
        }

        public decimal Maximum
        {
            get
            {
                return NmrInside.Maximum;
            }
            set
            {
                NmrInside.Maximum = value;
            }
        }

        /// <summary>
        /// 获取或设置文本框中的内容。
        /// </summary>
        [Category("设置")]
        [Description("获取或设置文本框中的内容。")]
        [Browsable(true)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
        public  decimal Value
        {
            get
            {
                return NmrInside.Value;
            }
            set
            {
                NmrInside.Value = value;
            }
        }


        /// <summary>
        /// 输入焦点丢失时，改变相关子控件的颜色。
        /// </summary>
        void TxtBox_Leave(object sender, EventArgs e)
        {
            BackColor = Color.White;
            PnlBorder.BackColor = Color.FromArgb(204, 204, 204);
            NmrInside.ForeColor = Color.FromArgb(102, 102, 102);
        }

        /// <summary>
        /// 获得输入焦点时，改变相关子控件的颜色。
        /// </summary>
        void TxtBox_GotFocus(object sender, EventArgs e)
        {
            BackColor = Color.FromArgb(179, 221, 150);
            PnlBorder.BackColor = Color.FromArgb(87, 152, 43);
            NmrInside.ForeColor = Color.FromArgb(51, 102, 0);
            NmrInside.Select(0, NmrInside.Text.Length);
            
        }

        /// <summary>
        /// 可以手动通过该方法，使TextBox获得输入焦点。
        /// </summary>
        public new void Focus()
        {
            NmrInside.Select();
        }

        /// <summary>
        /// TextBox控件大小改变时的事件处理。
        /// </summary>
        protected override void OnSizeChanged(EventArgs e)
        {
            base.OnSizeChanged(e);
            this.Height = 21;
            NmrInside.Location = new Point(2, 1);
            NmrInside.Width = PnlWhiteBG.Width - 1;
            NmrInside.Height = PnlWhiteBG.Height - 3;
        }


    }
}
